package My.project.Ui.Activity;

import My.project.Ui.R;
import My.project.Ui.TabListeners;
import My.project.Ui.Fragment.FriendFragment;
import My.project.Ui.Fragment.PeopleFragment;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ActionBar.Tab;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

/**
 * This activity is used to display social relationships of the user,
 * including friends and other users, which are displayed in two separate
 * fragments, combined by the action bar.
 * It can return to the MainActivity by click the home button in the menu.
 * @author Bell
 *
 */
public class ListUserActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
        ActionBar actionBar = getActionBar();
        // enable home button
        actionBar.setDisplayHomeAsUpEnabled(true);
        // setup action bar for tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);

        //friends tab
        Tab tab = actionBar.newTab()
                .setText(R.string.tab_friend)
                .setTabListener(new TabListeners<FriendFragment>(
                        this, "friend", FriendFragment.class));
        actionBar.addTab(tab);

        //other users tab
        tab = actionBar.newTab()
            .setText(R.string.tab_people)
            .setTabListener(new TabListeners<PeopleFragment>(
                    this, "all users", PeopleFragment.class));
        actionBar.addTab(tab);
	}
	
	//menu button action
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, R.string.str_about);
		menu.add(0, 1, 1, R.string.str_exit);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case android.R.id.home:
            // This is called when the Home (Up) button is pressed
            // in the Action Bar.
            Intent parentActivityIntent = new Intent(
            		this, MainActivity.class);
            parentActivityIntent.addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(parentActivityIntent);
            finish();
            break;
		case 0:
			openOptionsDialog();
			break;
		case 1:
			exitOptionsDialog();
			break;
		default:
			break;
		}
		return true;
	}
	
	// show author info
	private void openOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_about)
		.setMessage(R.string.app_about_msg)
		.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		}).show();
	}

	// show exit dialog
	private void exitOptionsDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_exit)
		.setMessage(R.string.app_exit_msg)
		.setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				
			}
		})
		.setPositiveButton(R.string.str_yes, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialoginterface, int i){
				Intent intent = new Intent(Intent.ACTION_MAIN);
  	            intent.addCategory(Intent.CATEGORY_HOME);
  	            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  	            startActivity(intent);
  	            android.os.Process.killProcess(android.os.Process.myPid());
			}
		}).show();
	}
}
